﻿using Cysharp.Threading.Tasks;
using UnityEngine;
using UnityEngine.UI;

public static partial class Extensions
{
    /// <summary>
    /// 异步加载 image.sprite 资源
    /// </summary>
    public static async UniTask LoadSpriteAsync(this Image image, string spriteName, string fallback = null, bool setNativeSize = false)
    {
        image.enabled = false;
        image.sprite = null;
        if (string.IsNullOrEmpty(spriteName)) return;
        var sprite = await AssetManager.LoadAsync<Sprite>(spriteName);
        if (sprite == null && !string.IsNullOrEmpty(fallback))
        {
            sprite = await AssetManager.LoadAsync<Sprite>(fallback);
        }
        if (image == null) return;
        image.sprite = sprite;
        if (image.sprite != null)
        {
            image.enabled = true;
            if (setNativeSize)
            {
                image.SetNativeSize();
            }
        }
        else
        {
            image.enabled = false;
        }
    }

    /// <summary>
    /// 同步加载 image.sprite 资源
    /// </summary>
    public static void LoadSprite(this Image image, string spriteName, string fallback = null, bool setNativeSize = false)
    {
        image.enabled = false;
        image.sprite = null;
        if (string.IsNullOrEmpty(spriteName)) return;
        var sprite = AssetManager.Load<Sprite>(spriteName);
        if (sprite == null && !string.IsNullOrEmpty(fallback))
        {
            sprite = AssetManager.Load<Sprite>(fallback);
        }
        if (image == null) return;
        image.sprite = sprite;
        if (image.sprite != null)
        {
            image.enabled = true;
            if (setNativeSize)
            {
                image.SetNativeSize();
            }
        }
        else
        {
            image.enabled = false;
        }
    }

    /// <summary>
    /// 异步加载 spriteRenderer.sprite 资源
    /// </summary>
    public static async UniTask LoadSpriteAsync(this SpriteRenderer spriteRenderer, string spriteName, string fallback = null)
    {
        spriteRenderer.enabled = false;
        spriteRenderer.sprite = null;
        if (string.IsNullOrEmpty(spriteName)) return;
        var sprite = await AssetManager.LoadAsync<Sprite>(spriteName);
        if (sprite == null && !string.IsNullOrEmpty(fallback))
        {
            sprite = await AssetManager.LoadAsync<Sprite>(fallback);
        }
        if (spriteRenderer == null) return;
        spriteRenderer.sprite = sprite;
        spriteRenderer.enabled = spriteRenderer.sprite != null;
    }

    /// <summary>
    /// 同步加载 spriteRenderer.sprite 资源
    /// </summary>
    public static void LoadSprite(this SpriteRenderer spriteRenderer, string spriteName, string fallback = null)
    {
        spriteRenderer.enabled = false;
        spriteRenderer.sprite = null;
        if (string.IsNullOrEmpty(spriteName)) return;
        var sprite = AssetManager.Load<Sprite>(spriteName);
        if (sprite == null && !string.IsNullOrEmpty(fallback))
        {
            sprite = AssetManager.Load<Sprite>(fallback);
        }
        if (spriteRenderer == null) return;
        spriteRenderer.sprite = sprite;
        spriteRenderer.enabled = spriteRenderer.sprite != null;
    }
}
